import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

const routes = [
  {
    path: '/',
    name: 'home',
    component: ()=>import('@/views/Words.vue')
  },
  // 注册
  {
    path: '/register',
    name: 'register',
    component: ()=>import('@/views/Register.vue')
  },
  // 登录
  {
    path: '/login',
    name: 'login',
    component: ()=>import('@/views/Login.vue')
  },
  // 更新单词
  {
    path: '/update',
    name: 'update',
    component: ()=>import('@/views/UpdateWord.vue')
  },
  // 添加单词
  {
    path: '/addword',
    name: 'addword',
    component: ()=>import('@/views/AddWord.vue')
  },
  // 修改密码
  {
    path: '/updpwd',
    name: 'updpwd',
    component: ()=>import('@/views/UpdatePwd.vue')
  }
]

const router = new Router({
  routes
})
router.beforeEach((to, form, next) => {
  let token = localStorage.getItem('token')
  if (to.path == '/') {
    if (token) {
      next()
    } else {
      next({
        path:'/login'
      })
    }
  } else {
    next()
  }
})

export default router
